package org.dromara.pay.service;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.business.domain.User;
import org.dromara.business.service.UserService;
import org.dromara.common.core.service.WxOrderService;
import org.dromara.pay.constant.PayWaysConstant;
import org.dromara.pay.domain.TopUp;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 充值成功回调处理器
 *
 * @author zlj
 * @since 2024/5/16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BusinessHandleService {

    private final WxOrderService wxOrderService;
    private final UserService userService;

    /**
     * 支付业务处理
     *
     * @param topUp 订单信息
     */
    public void businessHandle(TopUp topUp) {
        String orderType = topUp.getOrderType();
        String orderJson = topUp.getOrderJson();
        Long userId = topUp.getCreateBy();
        User user = userService.getById(userId);
        // TODO: 根据 业务订单类型 orderType 和 订单json orderJson自行处理逻辑
        // 计算当前时间加上指定天数后的时间
        //是购买会员
        if (ObjectUtil.equals(orderType, "goods")) {
            LocalDateTime currentTime = LocalDateTime.now();
            LocalDateTime newTime = currentTime.plusDays(30);
            Date gqTime = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
            user.setGqTime(gqTime);
            if (null==user.getHyNumber()){
                user.setHyNumber(0);
            }
            user.setHyNumber(user.getHyNumber()+1);
            boolean b = userService.updateById(user);
            //更新物流信息
            if (ObjectUtil.equals(topUp.getPayWay(), PayWaysConstant.WX_XCX_PAY)) {
                wxOrderService.uploadShippingInfo(Long.valueOf(orderJson));
            }
        }

    }

    /**
     * 退款业务处理
     *
     * @param topUp 订单信息
     */
    public void businessRefundHandle(TopUp topUp) {
        String orderType = topUp.getOrderType();
        String orderJson = topUp.getOrderJson();
        Long userId = topUp.getCreateBy();
        // TODO: 根据 业务订单类型 orderType 和 订单json orderJson自行处理逻辑
        if (ObjectUtil.equals(orderType, "1")) {
            // 1类型业务处理
        } else if (ObjectUtil.equals(orderType, "2")) {
            // 2类型业务处理
        }
    }
}
